package co.edu.poli.ficb.agl.automatable.gui;

import java.awt.event.ItemEvent;

import org.apache.commons.collections15.Factory;

import co.edu.poli.ficb.agl.automatable.modelo.automatas.AutomataFinitoImpl;
import co.edu.poli.ficb.agl.automatable.modelo.automatas.Estado;
import co.edu.poli.ficb.agl.automatable.modelo.automatas.TransicionImpl;
import edu.uci.ics.jung.visualization.RenderContext;
import edu.uci.ics.jung.visualization.control.EditingModalGraphMouse;
import edu.uci.ics.jung.visualization.renderers.LabelConfiguration;

/**
 * @author Diego Satoba
 */

enum MyMode {
	VIEWING, TRANSFORMING, PICKING, ANNOTATING, EDITING
}

public class MyEditingModalGraphMouse extends EditingModalGraphMouse<Estado, TransicionImpl>{
	
	private AutomataFinitoImpl automata;

	public MyEditingModalGraphMouse(AutomataFinitoImpl automata, RenderContext<Estado,TransicionImpl> rc,
			Factory<Estado> vertexFactory, Factory<TransicionImpl> edgeFactory, LabelConfiguration labelConfiguration) {
		super(rc, vertexFactory, edgeFactory);
		System.out.println("automata:"+automata);
		this.automata = automata;
		//loadPlugins();
		labelEditingPlugin = new MyLabelEditingGraphMousePlugin(automata, labelConfiguration);
		remove(popupEditingPlugin);
		popupEditingPlugin = new MyPopupGraphMousePlugin(automata, vertexFactory, edgeFactory);
		add(popupEditingPlugin);
	}
	
	public void setViewingMode() {
		remove(pickingPlugin);
		remove(animatedPickingPlugin);
		remove(translatingPlugin);
		remove(scalingPlugin);
		remove(rotatingPlugin);
		remove(shearingPlugin);
		remove(editingPlugin);
		remove(labelEditingPlugin);
		remove(annotatingPlugin);
		remove(popupEditingPlugin);
	}
	
	private static boolean isMode(MyMode mode) {
		for (Mode m : Mode.values()) {
			if (m.name().equals(mode.name()))
				return true;
		}
		return false;
	}
	
    public void setMode(MyMode mode) {
		if (isMode(mode)) {
			super.setMode(Mode.valueOf(mode.name()));
		} else {
			if (mode == MyMode.VIEWING) {
				setViewingMode();
			}
			if(modeBox != null) {
				modeBox.setSelectedItem(mode);
			}
			fireItemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED, mode, ItemEvent.SELECTED));
		}
	}
	/*
	@Override
	protected void loadPlugins() {
		super.loadPlugins();		
		labelEditingPlugin = new MyLabelEditingGraphMousePlugin();
		
		System.out.println("my loadPlugins");
		
		remove(popupEditingPlugin);
		popupEditingPlugin = new MyPopupGraphMousePlugin(automata, vertexFactory, edgeFactory);
		add(popupEditingPlugin);
	}*/
}
